from flask import Blueprint, jsonify, request

from service.UploadService import save_file_to_local, save_file_to_oss

api_upload = Blueprint('api_upload', __name__)
api_upload.url_prefix = '/api'


@api_upload.route('/upload', methods=['GET', 'HEAD', 'OPTIONS', 'POST'])
def upload_file():
    file = request.files['editormd-image-file']
    result = {
        'code': 200,
        'msg': '图片上传成功',
        'data': ''
    }
    save_path, relative_file = save_file_to_local(file)
    if save_path == '':
        result['msg'] = '文件上传失败'
        result['code'] = 500
        return result
    else:
        result['data'] = save_file_to_oss(save_path, relative_file)
    return jsonify(result)